﻿// by Shimohana
using System.Collections.Generic;
using System.Windows;
using System.Xml.Linq;
using AzukiMap.Map;
using AzukiMap.MapSource;
using AzukiMap.Renderer;
using AzukiMap.Request;
using Geometries = GisSharpBlog.NetTopologySuite.Geometries;
using AzukiMap.Element;
using AzukiMap.Tools;

namespace AzukiMap.Layer
{
#if false
    public class MeshLayer : VectorLayer
    {
        MeshSource _meshSource;
        MeshRequest _meshRequest;
        MeshParser _meshParser;
        Dictionary<int, bool> _rendered;

        public MeshSource MeshSource
        {
            get { return _meshSource; }
            set { 
                _meshSource = value;
                SetRequest(_meshSource);
            }
        }

        public MeshLayer(MeshSource meshSource , string name, Geometries.Polygon extent)
            : base((VectorSource)meshSource, name, extent)
        {
            SetRequest(meshSource);
            _rendered = new Dictionary<int, bool>();
        }

        public MeshLayer(MeshSource meshSource, string name)
            : base((VectorSource)meshSource, name)
        {
            SetRequest(meshSource);
            _rendered = new Dictionary<int, bool>();
        }

        public MeshLayer()
            : base(new MeshSource(),"")
        {
            _rendered = new Dictionary<int, bool>();
        }

        void SetRequest(MeshSource meshSource)
        {
            _meshSource = meshSource;
            if (meshSource.GetColumnDataUri != null)
            {
                _meshRequest = new MeshRequest(this, meshSource.GetColumnDataUri);
            }
            else
            {
                _meshRequest = new MeshRequest(this);
            }
            _meshRequest.GetDataCompleted += new GetDataCompletedHandler(_meshRequest_GetDataCompleted);
        }

        public override void Draw()
        {
            if (!CheckDrawingStatus())
            {
                return;
            }
            IsRendering = true;
            _meshRequest.Request();
        }

        public override void Redraw()
        {
            if (!CheckDrawingStatus())
            {
                return;
            }
            IsRendering = true;
            MapCoord.ClearLastExtent();
            _meshRequest.Request();
        }

        bool CheckDrawingStatus()
        {
            if (IsRendering)
            {
                return false;
            }
            if (!IsVisible)
            {
                return false;
            }
            if (!CheckScale())
            {
                return false;
            }
            // by Shimohana
            if (!IsDrawable)
            {
                return false;
            }
            return true;
        }

        void _meshRequest_GetDataCompleted(object sender, GetDataCompletedArgs e)
        {
            _meshParser = new MeshParser(e.DataStream);
            int numOfGeom = _meshParser.NumOfObjects;
            MeshDataSetHeader header = _meshParser.MeshDataSet.Header;

            // by Shimohana
            Children.Clear();

            Geometries.Polygon polygon = new GisSharpBlog.NetTopologySuite.Geometries.Polygon(
                new Geometries.LinearRing(
                    new GeoAPI.Geometries.ICoordinate[] {
                        new Geometries.Coordinate(header.MaxX, header.MinY),
                        new Geometries.Coordinate(header.MaxX, header.MaxY),
                        new Geometries.Coordinate(header.MinX, header.MaxY),
                        new Geometries.Coordinate(header.MinX, header.MinY),
                        new Geometries.Coordinate(header.MaxX, header.MinY),
                    }
            ));
            IVectorStyle style = new VectorStyle();
            style.FillBrush = _meshParser.GetImageBrush();
            List<FrameworkElement> elems = _renderer.DrawGeometry(0, polygon, style);

            /*
            IVectorStyle vStyle;
            foreach (PostGisRecord record in _meshParser)
            {
                if (_rendered.ContainsKey(record.Gid))
                {
                    continue;
                }
                // No layer theme
                if (record.ThemeId == -1)
                {
                    vStyle = _defaultStyle;
                }
                else
                {
                    vStyle = _layerThemes[record.ThemeId].Style;
                }
                if (_selectedIds.ContainsKey(record.Gid))
                {
                    vStyle = _selectedStyle;
                }
                List<FrameworkElement> elems = _renderer.DrawGeometry(record.Gid, record.Geometry, vStyle);
                SetEventsOnElement(elems);

                if (record.Attributes != null )
                {
                    Label label = new Label();
                    label.Id = record.Gid;
                    label.Geom = record.Geometry;
                    label.Text = record.Attributes[0];
                    _labelLayer.Add(label);
                }

                _rendered.Add(record.Gid, true);
            }
             */

            IsRendering = false;
        }

        public override void Clear()
        {
            base.Clear();
            _rendered.Clear();
        }

        public override XElement ToXml()
        {
            return ToXml("PostGisLayer");
        }

        public override XElement ToXml(string elementName)
        {
            XElement vectorLayerXml = base.ToXml(elementName);
            return vectorLayerXml;
        }

        public override void Parse(XElement layerXml)
        {
            base.Parse(layerXml);
            MeshSource source = new MeshSource();
            source.Parse(layerXml.Element("MeshSource"));
            _meshSource = source;
            _data = source;
            MapSource = source;
            VectorType = source.VectorType;
            SetRequest(_meshSource);
        }
    }
#endif
}
